home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / Aidan's Class Libraries / Source / Cleanup / Cleanup.cpp
Encoding:
Text File  |  1997-05-24  |  5.1 KB  |  157 lines  |  [TEXT/CWIE]

  1. //Copyright (c) 1997 Aidan Cully
  2. //All rights reserved
  3.  
  4. #include <Dialogs.h>
  5. #include "CLCleanup.h"
  6.  
  7. //    TCleanup::RegisterDirtyAction()
  8. //    uses:
  9. //        Use this procedure to place a procedure in the procedure list which will be called
  10. //        after two conditions are met:
  11. //            1: that an action is pending in the "Dirty" list (normally this occurs through
  12. //             calls to "NotifyDirty", which must be called after "RegisterDirtyAction"
  13. //            2: that Clean be called.  This can be called from anywhere, but will most likely
  14. //             be called (depending on the variable you use for cleanup) every time through the
  15. //             the event loop.
  16. //    in:
  17. //        UInt32 procID: a unique ID number which is forever bound to the second parameter
  18. //        CleanupProc theProc: a procedure which takes a void* and a procID as parameters, and
  19. //         performs some action or other based on these datas.
  20. //    return value:
  21. //        UInt8: an error code.  Current possibilities are:
  22. //            kSuccess == No Error
  23. //            kDuplicateActionErr == the procID variable is already in use
  24. //            kCleanupMemErr == out of memory
  25. UInt8 TCleanup::RegisterDirtyAction( UInt32 procID, CleanupProc theProc ) {
  26.     TProcEl *theEl;
  27.  
  28.     //if nothing is in the procedure list as yet, then insert the first element.
  29.     if( !mProcs.MoveFirst() ) {
  30.         //create and initialize the element structure.  We need to have an element structure
  31.         //because of the *extremely* rudimentary way in which TList operates.
  32.         theEl = new TProcEl;
  33.         if( !theEl )
  34.             return( kCleanupMemErr );
  35.         theEl->mProcID = procID;
  36.         theEl->mProc = theProc;
  37.         if( !mProcs.AddNext( theEl ) )
  38.             return( kCleanupMemErr );
  39.         return( kSuccess );
  40.     } else { //otherwise, we have to check that the procID variable is actually unique before
  41.              //inserting.
  42.         //the if block above called upon entry "procs.MoveFirst()," so we don't have to bother
  43.         //here.
  44.         UInt32 temp;
  45.         do {    //loop through every element in the list to find possibly duplicate procID's.
  46.             mProcs.GetData( theEl );
  47.             if( theEl->mProcID == procID )    //Found one?
  48.                 return( kDuplicateActionErr );    //then return the error code.
  49.         } while( mProcs.MoveNext() );
  50.         //create the new variables (in the TList, and in the ProcType*) checking for memory
  51.         //allocation errors.
  52.         theEl = new TProcEl;
  53.         if( !theEl )
  54.             return( kCleanupMemErr );
  55.         theEl->mProcID = procID;
  56.         theEl->mProc = theProc;
  57.         if( !mProcs.AddNext( theEl ) )
  58.             return( kCleanupMemErr );
  59.         return( kSuccess );
  60.     }
  61. }
  62.  
  63. //    TCleanup::NotifyDirty()
  64. //    uses:
  65. //        Use NotifyDirty to have a function called with certain data the next time Clean() is
  66. //        called for this particular instance of TCleanup.
  67. //    in:
  68. //        UInt32 procID: unique ID number identifying which procedure to call
  69. //        void *procData: data to be used locally by said procedure
  70. //    return value:
  71. //        UInt8: possible return values are at present:
  72. //            kSuccess == everything's groovin.
  73. //            kActionNotFoundErr == the procID specified is invalid.
  74. //            kCleanupMemErr == out of memory.
  75. UInt8 TCleanup::NotifyDirty( UInt32 procID, void *procData ) {
  76.     TDataEl *theEl;
  77.     TProcEl *procEl;
  78.     Boolean found = false;
  79.  
  80.     //Verify that a procedure IS identified by procID.
  81.     if( !mProcs.MoveFirst() )
  82.         return( kActionNotFoundErr );
  83.     UInt32 temp;
  84.     do {
  85.         mProcs.GetData( procEl );
  86.         if( procEl->mProcID == procID )
  87.             found = true;
  88.     } while( !found && mProcs.MoveNext() );
  89.     if( !found )
  90.         return( kActionNotFoundErr );
  91.     //The procedure does exist, now add an element to the stack signifying that this procedure
  92.     //must be called with this particular data.
  93.     theEl = new TDataEl;
  94.     if( !theEl )
  95.         return( kCleanupMemErr );
  96.     theEl->mProcID = procID;
  97.     theEl->mData = procData;
  98.     if( !mQueue.Enqueue( theEl ) )
  99.         return( kCleanupMemErr );
  100.     //Wow, everything went fine!
  101.     return( kSuccess );
  102. }
  103.  
  104. //    TCleanup::Clean()
  105. //    uses:
  106. //        Use this procedure to call all other procedures that the TCleanup object has been told
  107. //        to call (usually by use of the NotifyDirty() procedure).
  108. //    in: none.
  109. //    return value:
  110. //        UInt8: the return value can be (at present) one of the following:
  111. //            kSuccess == Far Out in a Happening Kind of Way (™)
  112. //            kActionNotFound == one of the elements in the list had no appropriate action.
  113. //            kActionLocalErr == the action procedure called had some kind of problem with it.
  114. UInt8 TCleanup::Clean() {
  115.     TDataEl *theEl;
  116.     TProcEl *theProc;
  117.     UInt32 temp;
  118.     Boolean found;
  119.  
  120.     while( mQueue.Dequeue( theEl ) ) {
  121.         if( !mProcs.MoveFirst() )
  122.             return( kActionNotFoundErr );
  123.         found = false;
  124.         do {
  125.             mProcs.GetData( theProc );
  126.             if( theProc->mProcID == theEl->mProcID )
  127.                 found = true;
  128.             else if( !mProcs.MoveNext() ) {
  129.                 delete theEl;
  130.                 return( kActionNotFoundErr );
  131.             }
  132.         } while( !found );
  133.         if( !theProc->mProc( theEl->mProcID, theEl->mData ) ) {
  134.             delete theEl;
  135.             return( kActionLocalErr );
  136.         }
  137.         delete theEl;
  138.     }
  139.     return( kSuccess );
  140. }
  141.  
  142. //    TCleanup::~TCleanup()
  143. //    Destroys all variables associated with TCleanup.  DOES NOT call the action procedures (at
  144. //    this point, that could even be dangerous).
  145. TCleanup::~TCleanup() {
  146.     TProcEl *theProc;
  147.     TDataEl *theData;
  148.  
  149.     if( mProcs.MoveFirst() )
  150.         do {
  151.             mProcs.GetData( theProc );
  152.             delete theProc;
  153.         } while( mProcs.Remove() );
  154.     while( mQueue.Dequeue( theData ) ) {
  155.         delete theData;
  156.     }
  157. }